class Solution
{
public:
    long long dividePlayers(vector<int> &skill)
    {
        int n = skill.size();
        if (n & 1)
        {
            return -1;
        }
        int skillSum = 0;
        unordered_map<int, int> skillCount;

        for (int num : skill)
        {
            skillSum += num;
            ++skillCount[num];
        }
        if (skillSum % (n / 2) != 0)
        {
            return -1;
        }
        int eachSkill = skillSum / (n / 2);
        long long power = 0;
        for (int num : skill)
        {
            if (skillCount[num] == 0)
            {
                continue;
            }
            if (!skillCount.count(eachSkill - num) || skillCount[eachSkill - num] == 0)
            {
                return -1;
            }
            power += static_cast<long long>(num) * (eachSkill - num);
            --skillCount[num];
            --skillCount[eachSkill - num];
        }
        return power;
    }
};